package com.example.easeimpro.controller.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.easeimpro.R;
import com.example.easeimpro.controller.activity.ChatSendMsgActivity;
import com.example.easeimpro.controller.activity.CreateGroupActivity;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.adapter.EMAGroup;
import com.hyphenate.easeui.constants.EaseConstant;

import java.util.ArrayList;
import java.util.List;

public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.MyHolder> {
    private Context mContext;
    private List<EMGroup> mGroupList = new ArrayList<>();
    public GroupListAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void refresh(List<EMGroup> groups){
        if(groups != null && groups.size()>0){
            mGroupList.clear();
            mGroupList.add(new EMGroup(new EMAGroup()));
            mGroupList.addAll(groups);
            notifyDataSetChanged();
        }else {
            mGroupList.add(new EMGroup(new EMAGroup()));
            notifyDataSetChanged();
        }
    }

    private int LIST_HEADER = 0;

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return LIST_HEADER;
        }
        return -1;
    }

    private static final String TAG = "GroupListAdapter";
    @NonNull
    @Override
    public GroupListAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == LIST_HEADER){
            return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.group_list_header_item,parent,false));
        }
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.group_list_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull GroupListAdapter.MyHolder holder,  int position) {
        int pos = position;
        if(position == 0){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CreateGroupActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }else {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e(TAG, "onClick: " );
                    Intent intent = new Intent(mContext,ChatSendMsgActivity.class);
                    intent.putExtra("conversationId",mGroupList.get(pos).getGroupId());
               intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP);
               mContext.startActivity(intent);
                }
            });
            holder.mGroupText.setText(mGroupList.get(pos).getGroupName());
        }



    }

    @Override
    public int getItemCount() {
        Log.e(TAG, "getItemCount: "+mGroupList.size());
        return mGroupList == null ? 0:mGroupList.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView mGroupText;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            mGroupText = itemView.findViewById(R.id.group_item_text);
        }
    }
}
